package com.haiqi.modules.message.service;

import com.github.pagehelper.PageInfo;
import com.haiqi.common.bean.PageBean;
import com.haiqi.common.persistence.dao.MessageMapper;
import com.haiqi.common.persistence.entity.Message;
import com.haiqi.common.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by hythzx on 2016/6/21.
 */
@Service
public class MessageService {

    @Autowired
    private MessageMapper messageMapper;

    public PageInfo<Message> getDataList(Message message, PageBean page){
        PageUtils.generatePage(page);
        List<Message> dataList = messageMapper.getDataList(message);
        PageInfo<Message> shipInfoList = new PageInfo<Message>(dataList);
        return shipInfoList;
    }

    public List<Message> getDataList(Message message){
        List<Message> dataList = messageMapper.getDataList(message);
        return dataList;
    }

    public Message getMessageInfo(int id){
        Message message = messageMapper.selectByPrimaryKey(id);
        return message;
    }

    public boolean update(Message message){
        boolean success = true;
        int count = 0;
        if(message.getId() == null){
            String shipid = message.getShipid();
            if(shipid != null){
                String[] ships = shipid.split(",");
                for (String ship : ships) {
                    message.setShipid(ship);
                    count = messageMapper.insertSelective(message);
                    if(count == 0){
                        success = false;
                    }
                }
            }
        }else{
            count = messageMapper.updateByPrimaryKeySelective(message);
            if(count == 0){
                success = false;
            }
        }
        return success;
    }

    public boolean delete(int id){
        boolean success = false;
        int count = 0;
        count = messageMapper.deleteByPrimaryKey(id);
        if(count > 0){
            success = true;
        }
        return success;
    }


    public boolean deleteArray(String id){
        boolean success = false;
        int count = 0;
        String[] ids = id.split(",");
        int array [] = new int[ids.length];
        for (int i = 0; i < ids.length; i++) {
            array[i] = Integer.valueOf(ids[i]);
        }
        count = messageMapper.deleteArray(array);
        if(count > 0){
            success = true;
        }
        return success;
    }
}
